import { ColumnProps } from '@/components/ProTable/interface';
import { ProTableValue } from '@/interface/public';

/**
 * @param { Array<any>} dataList 表格数据
 * @param { string} mergeField 合并的字段
 * @description 合并列
 */
export const tableMergeColumn = (dataList: any, mergeField: string, flag = 'rowSpan') => {
  const length = dataList.length;
  if (!length) {
    return [];
  }

  for (let i = 0; i < dataList.length; i++) {
    dataList[i][flag] = 1;
    for (let j = i + 1; j < dataList.length; j++) {
      if (dataList[i][mergeField] === dataList[j][mergeField]) {
        dataList[i][flag]++;
        dataList[j][flag]--;
      }
    }
    // 这里跳过已经重复的数据
    i = i + dataList[i][flag] - 1;
  }
};
/**
 * @param {any}  data 需要导出的对象
 * @param {String}  title 导出的标题
 * @returns 导出
 */
export const tableExportData = (data: BlobPart, title: string, flag: boolean = false) => {
  if (!data) return;
  const blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=utf-8' });
  const url = window.URL.createObjectURL(blob);
  const aLink = document.createElement('a');
  aLink.style.display = 'none';
  aLink.href = url;
  aLink.setAttribute('download', `${title}${!flag ? '.xlsx' : ''}`);
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink);
  window.URL.revokeObjectURL(url);
};

export const _clearTableData = (arr: Array<ProTableValue>) => {
  for (let item of arr) {
    if (item.value) {
      item.value.tableData = [];
      item.value.originData = [];
    }
  }
};

/**
 * @param {ColumnProps} columns 表格的列
 * @param {String} props 需要修改的字段 (多个以逗号隔开)
 * @param {boolean}  flag 隐藏 、显示
 * @description 修改表格中查询表单显示逻辑
 */
export const tableChangeSearchForm = async (columns: any, props = '', flag = false) => {
  const prop = props.split(',');
  for (let item of columns) {
    let key = item.search?.key || '';
    if (prop.includes(key) && item.search) {
      item.search.hide = flag;
    }
  }
};

/**
 * 设置数据字典中enum
 */
export const tableSetDictionariesEnum = async tableData => {
  const code = arrJoinToStr(tableData.transitionArr, 'oldField');
  const enumArr = await getDictionaryListByCodes(code);
  console.log(enumArr, 'enumArr', tableData);

  let children = new Map();
  for (let item of tableData.transitionArr) {
    if (enumArr[item.key]) {
      children.set(item.newField, enumArr[item.key]);
    } else {
      children.set(item.newField, enumArr);
    }
  }

  tableSetChildrenEnum(tableData, children);
};

/**
 * 将children直接设置到表格enum中
 */
export const tableSetChildrenEnum = async (tableData, children: Map<String, any[]>) => {
  let count = 0;
  let length = children.size;
  for (let col of tableData.columns) {
    for (let [key, value] of children) {
      if (col.prop == key) {
        col.enum = value;
        count++;
        if (count == length) {
          return;
        }
      }
    }
  }
};

/**
 * 转换enum的值
 */
export const tableTransitionEnum = (e: any[], value: any) => {
  return e.find(item => item.value == value)?.label || value;
};
